/**
 * @name GlobalHeader/SlsCompany.tsx
 * @desc
 * @author yangliu at 2019-11-25
 *
 */
import React from 'react';
import { DownOutlined } from '@ant-design/icons';
import { Menu, Dropdown } from 'antd';
import styles from './index.less';
import { getUserInfo, setUserInfo } from '@/utils/userInfo';
interface ComponentProps {
  menu?: boolean;

}
interface ComponentState {
  SlsCompanyCode: string;
  SlsChannelCode: string;
}

class SlsCompany extends React.Component<ComponentProps, ComponentState> {
  constructor(props: ComponentProps) {
    const currentUser: any = getUserInfo();
    super(props);
    this.state = {
      SlsCompanyCode: currentUser.SlsCompanyCode,
      SlsChannelCode: currentUser.SlsChannelCode,
    };
  }

  render() {
    const currentUser = getUserInfo();
    const { SlsCompany, SlsChannel } = currentUser;
    const { SlsCompanyCode, SlsChannelCode } = this.state;
    const SlsCompanyNow = SlsCompany && SlsCompany.filter((item) => item.code === SlsCompanyCode);
    const SlsChannelNow = SlsChannel && SlsChannel.filter((item) => item.code === SlsChannelCode);
    const SlsCompanyMenu = (
      <Menu
        className={styles.menu}
        selectedKeys={[SlsCompanyCode]}
        onClick={(item: any) => {
          this.setState({ SlsCompanyCode: item.key });
          const currentUser: any = getUserInfo();
          setUserInfo({ ...currentUser, SlsCompanyCode: item.key });

        }}
      >
        {(SlsCompany || []).map((item: any) => (
          <Menu.Item key={item.code}>{item.name}</Menu.Item>
        ))}
      </Menu>
    );
    const SlsChannelMenu = (
      <Menu
        className={styles.menu}
        selectedKeys={[SlsChannelCode]}
        onClick={(item: any) => {
          this.setState({ SlsChannelCode: item.key });
          const currentUser: any = getUserInfo();
          setUserInfo({ ...currentUser, SlsChannelCode: item.key });

        }}
      >
        {(SlsChannel || []).map((item: any) => (
          <Menu.Item key={item.code}>{item.name}</Menu.Item>
        ))}
      </Menu>
    );

    return (
      <span className="slscompanyChannel">
        {SlsCompanyNow && SlsCompanyNow.length === 1 ? (
          <Dropdown overlay={SlsCompanyMenu} key={1}>
            <span>
              {SlsCompanyNow[0].name} <DownOutlined />
            </span>
          </Dropdown>
        ) : null}
        {SlsChannelNow && SlsChannelNow.length === 1 ? (
          <Dropdown overlay={SlsChannelMenu} key={1}>
            <span>
              {SlsChannelNow[0].name} <DownOutlined />
            </span>
          </Dropdown>
        ) : null}
      </span>
    );
  }
}
export default SlsCompany;
